package org.pc2r.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

public class FrontStreamHandler extends StreamHandler {

	public FrontStreamHandler() {
		super(System.err, new Handler());
	}
	
	static public class Handler extends Formatter {

		@Override
		public String format(LogRecord record) {
			String result = record.getLevel() + ": " + record.getMessage();
			String throwable = null;
			if(record.getThrown() != null) {
				StringWriter sw = new StringWriter();
	            PrintWriter pw = new PrintWriter(sw);
	            pw.println();
	            record.getThrown().printStackTrace(pw);
	            pw.close();
	            throwable = sw.toString();
	            return result + "\n" + throwable;
			}
			return result;
		}
		
	}

}
